<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="患者名称" prop="outpatientid">
        <el-input v-model="queryParams.huanzhe" placeholder="请输入患者名称" clearable size="small" style="width: 120px;"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="医生名称" prop="doctorid">
        <el-input v-model="queryParams.yisheng" placeholder="请输入医生名称" clearable size="small" style="width: 120px;"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="诊断状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择诊断状态" clearable size="small" style="width: 150px;">
          <el-option v-for="dict in dict.type.outpatientDoctor_receiveRecord_status" :key="dict.value"
            :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker clearable size="small" v-model="queryParams.createTime" type="date" value-format="yyyy-MM-dd"
          style="width: 150px;" placeholder="选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="接诊状态" prop="jiezhen">
        <el-select v-model="queryParams.jiezhen" placeholder="请选择接诊状态" clearable size="small" style="width: 150px;">
          <el-option v-for="dict in dict.type.outpatientDoctor_receiveRecord_jiezhen" :key="dict.value"
            :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:receiverecord:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain size="mini" @click="jiezhen">接诊</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain size="mini" @click="shunyan">顺延</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain size="mini" @click="handleAdd">检查</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain size="mini" @click="handleAdd">检验</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain size="mini" @click="handleAdd">处置</el-button>
      </el-col> -->
      <el-button type="warning" plain icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleExport"
        v-hasPermi="['system:receiverecord:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="receiverecordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" width="50">
        <template slot-scope="scope">
          <div>{{scope.$index|rowNumFiler(queryParams.pageNum,queryParams.pageSize)}}</div>
        </template>
      </el-table-column>
      <el-table-column label="患者" align="center" prop="outpatientid" />
      <el-table-column label="医生" align="center" prop="sysUsery.nickName" />
      <el-table-column label="诊断状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.outpatientDoctor_receiveRecord_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="接诊状态" align="center" prop="jiezhen">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.outpatientDoctor_receiveRecord_jiezhen" :value="scope.row.jiezhen" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:receiverecord:edit']">修改</el-button> -->
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:receiverecord:remove']">删除</el-button>
          <span v-if="scope.row.jiezhen==1" style="margin-left: 10px;">
            <el-button label="rtl" size="mini" type="text" @click="jiancha(scope.row)">检查</el-button>
          </span>
          <span v-if="scope.row.jiezhen==2" style="margin-left: 10px;">
            <el-button label="rtl" size="mini" type="text" @click="zhenduan(scope.row)">诊断</el-button>
          </span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改接诊记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="患者名称" prop="outpatientid">
          <el-input v-model="form.outpatientid" placeholder="请输入患者名称" />
        </el-form-item>
        <el-form-item label="医生名称" prop="doctorid">
          <el-select v-model="form.doctorid" placeholder="请选择主治医生" style="width: 380px;">
            <el-option label="张三" value="admin"></el-option>
            <el-option label="李四" value="ry"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="诊断状态" prop="status" style="width: 600px;">
          <el-select v-model="form.status" placeholder="请选择诊断状态" style="width: 380px;">
            <el-option v-for="dict in dict.type.outpatientDoctor_receiveRecord_status" :key="dict.value"
              :label="dict.label" :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="接诊状态" prop="jiezhen">
          <el-select v-model="form.jiezhen" placeholder="请选择接诊状态" style="width: 380px;">
            <el-option v-for="dict in dict.type.outpatientDoctor_receiveRecord_jiezhen" :key="dict.value"
              :label="dict.label" :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-drawer title="检查" :visible.sync="drawer" :direction="direction" :before-close="handleClose">
      <span>
        <center>
          <el-form ref="form" :model="lisinfo" label-width="80px">
            <el-form-item label="患者名称">
              <el-input v-model="lisinfo.huanzhe" disabled style="width: 300px;"></el-input>
            </el-form-item>
            <el-form-item label="操作人">
              <el-input v-model="lisinfo.yisheng" disabled style="width: 300px;"></el-input>
            </el-form-item>
            <el-form-item label="主治医生">
              <el-select v-model="lisinfo.zhuzhiyisheng" placeholder="请选择主治医生" style="width: 300px;">
                <el-option label="张三" value="张三"></el-option>
                <el-option label="李四" value="李四"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="检查事项">
              <el-autocomplete style="width: 300px;" v-model="lisinfo.state" :fetch-suggestions="querySearchAsync"
                placeholder="请输入内容" @select="handleSelect"></el-autocomplete>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="onSubmit">提交数据</el-button>
              <el-button>取消</el-button>
            </el-form-item>
          </el-form>
        </center>
      </span>
    </el-drawer>
    <el-dialog title="患者诊断" :visible.sync="dialogFormVisible" width="400px">
      <el-form :model="zhenduanjieguo" ref="zhenduanjieguo" :rules="ruless">
        <el-form-item label="患者"prop="huanzhe">
          <el-input v-model="zhenduanjieguo.huanzhe" style="width: 300px;" disabled autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="医生"prop="yisheng">
          <el-input v-model="zhenduanjieguo.yisheng" style="width: 300px;" disabled autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="诊断结果"prop="name">
          <el-autocomplete v-model="zhenduanjieguo.name" style="width: 270px;" :fetch-suggestions="querySearch"
            placeholder="请输入内容" @select="handleSelect">

            <template slot-scope="{ item }">
              <div>{{ item.value }}</div>
              <span>{{ item.address }}</span>
            </template>
          </el-autocomplete>
        </el-form-item>
        <el-form-item label="描述症状"prop="description">
          <el-select style="width: 270px;" v-model="zhenduanjieguo.description" multiple filterable allow-create
            default-first-option placeholder="请选择患者症状">
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="医嘱"prop="remark">
          <el-input v-model="zhenduanjieguo.remark" style="width: 300px;" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm2('zhenduanjieguo')">取 消</el-button>
        <el-button type="primary" @click="zhenduandata('zhenduanjieguo')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listReceiverecord,
    getReceiverecord,
    delReceiverecord,
    addReceiverecord,
    updateReceiverecord,
    exportReceiverecord,
    editzhenduan, //接诊顺延
    jiancha, //检查
    zhenduans, //诊断
  } from "@/api/system/receiverecord";

  export default {
    name: "Receiverecord",
    dicts: ['outpatientDoctor_receiveRecord_status', 'outpatientDoctor_receiveRecord_jiezhen'],
    data() {
      return {
        // 遮罩层
        options: [{
          value: '皮肤感染',
          label: '皮肤感染'
        }, {
          value: '乏力',
          label: '乏力'
        }, {
          value: '高血糖',
          label: '高血糖'
        }, {
          value: '高血压',
          label: '高血压'
        }, {
          value: '恶心呕吐',
          label: '恶心呕吐'
        }, {
          value: '出冷汗',
          label: '出冷汗'
        }, {
          value: '伤口感染',
          label: '伤口感染'
        }, {
          value: '过敏',
          label: '过敏'
        }, {
          value: '真菌感染',
          label: '真菌感染'
        }, {
          value: '四肢无力',
          label: '四肢无力'
        }, {
          value: '发烧',
          label: '发烧'
        }, {
          value: '便秘',
          label: '便秘'
        }, {
          value: '视力模糊',
          label: '视力模糊'
        }, {
          value: '皮肤瘙痒',
          label: '皮肤瘙痒'
        }, {
          value: '头晕',
          label: '头晕'
        }, {
          value: '腹泻',
          label: '腹泻'
        }],
        loading: true,
        form: {
          name: '',
          region: '',
          date1: '',
          date2: '',
          delivery: false,
          type: [],
          resource: '',
          desc: ''
        },
        drawer: false,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        dialogFormVisible: false,
        // 总条数
        total: 0,
        // 接诊记录表格数据
        receiverecordList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          outpatientid: null,
          doctorid: null,
          status: null,
          createTime: null,
          jiezhen: null,
          yisheng: null, //医生
          huanzhe: null, //患者
          zhuzhiyisheng: null, //主治医师
          state: null, //检查事项
        },
        lisinfo: {
          id: null,
          yisheng: null, //医生
          huanzhe: null, //患者
          zhuzhiyisheng: null, //主治医师
          state: null, //检查事项
        },
        Obj: {
          idss: [],
          status: null
        },
        direction: 'rtl',
        // 表单参数
        form: {},
        ruless: {
          name: [{
            required: true,
            message: '请选择诊断结果',
            trigger: 'change'
          }],
          description: [{
            required: false,
            message: '请选择描述症状',
            trigger: 'change'
          }],remark: [{
          required: true,
          message: '请输入医嘱',
          trigger: 'blur'
        }],
        },



        rules: {
          outpatientid: [{
            required: true,
            message: '请输入患者名称',
            trigger: 'blur'
          }],
          doctorid: [{
            required: true,
            message: '请选择性别',
            trigger: 'change'
          }],
          status: [{
            required: true,
            message: '请选择诊断状态',
            trigger: 'change'
          }],
          jiezhen: [{
            required: true,
            message: '请选择接诊状态',
            trigger: 'change'
          }],

        },
        restaurants: [],
        restaurantss: [],
        state: '',
        timeout: null,
        zhenduanjieguo: {
          name: null, //诊断结果
          description: null, //描述症状//医嘱
          remark: null, //医嘱
          opdoctorreceiverecordid: null, //接诊记录
          zddate: null, //诊断时间
          yisheng: null, //医生
          huanzhe: null //患者
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询接诊记录列表 */
      getList() {
        this.loading = true;
        listReceiverecord(this.queryParams).then(response => {
          this.receiverecordList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          opdoctorreceiverecordid: null,
          outpatientid: null,
          doctorid: null,
          status: null,
          createTime: null,
          jiezhen: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.queryParams.jiezhen = null;
        this.queryParams.yisheng = null;
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.opdoctorreceiverecordid)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加接诊记录";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const opdoctorreceiverecordid = row.opdoctorreceiverecordid || this.ids
        getReceiverecord(opdoctorreceiverecordid).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改接诊记录";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.opdoctorreceiverecordid != null) {
              updateReceiverecord(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addReceiverecord(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const opdoctorreceiverecordids = row.opdoctorreceiverecordid || this.ids;
        this.$confirm('是否确认删除接诊记录编号为"' + opdoctorreceiverecordids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delReceiverecord(opdoctorreceiverecordids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有接诊记录数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportReceiverecord(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
      },
      jiezhen() {
        this.Obj.status = 2;
        this.editzhenduan(this.Obj)
      },
      shunyan() {
        this.Obj.status = 3;
        this.editzhenduan(this.Obj)
      },
      editzhenduan(Obj) {
        Obj.idss = this.ids
        editzhenduan(Obj).then(response => {
          var name = "";
          if (Obj.status == 2) {
            name = '接诊';
          } else if (Obj.status == 3) {
            name = '延期';
          }
          this.msgSuccess(name + "成功");
          this.getList()
        });
      },
      jiancha(row) {
        this.drawer = true;
        this.lisinfo.id = row.opdoctorreceiverecordid;
        this.lisinfo.huanzhe = row.outpatientid;
        this.lisinfo.yisheng = row.sysUsery.nickName;
      },
      handleClose(done) {
        done();

      },



      querySearchAsync(queryString, cb) {
        var restaurants = this.restaurantss;
        var results = queryString ? restaurants.filter(this.createStateFilter(queryString)) : restaurants;

        clearTimeout(this.timeout);
        this.timeout = setTimeout(() => {
          cb(results);
        }, 3000 * Math.random());
      },
      createStateFilter(queryString) {
        return (state) => {
          return (state.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
        };
      },
      handleSelect(item) {},
      loadAll() {
        return [{
          "value": "血常规"
        }, {
          "value": "尿检"
        }, {
          "value": "皮试"
        }, ]
      },
      loadAll2() {
        return [{
          "value": "扁桃体发炎"
        }, {
          "value": "月经失调"
        }, {
          "value": "感冒发烧"
        }, {
          "value": "流行性感冒"
        }, {
          "value": "小儿麻痹"
        }, {
          "value": "中风"
        }, {
          "value": "胃窦浅表性胃炎"
        }, {
          "value": "静脉曲张"
        }, ]
      },
      onSubmit() {
        jiancha(this.lisinfo).then(response => {
          this.msgSuccess("已将患者分配到LIS检查");
        });
        this.handleClose()
        this.getList();
      },
      zhenduan(row) {
        this.dialogFormVisible = true;
        this.zhenduanjieguo.opdoctorreceiverecordid = row.opdoctorreceiverecordid;
        // console.log(row)
        this.zhenduanjieguo.huanzhe = row.outpatientid;
        this.zhenduanjieguo.yisheng = row.sysUsery.nickName;

      },
      querySearch(queryString, cb) {
        var restaurants = this.restaurants;
        var results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants;
        // 调用 callback 返回建议列表的数据
        cb(results);
      },
      createFilter(queryString) {
        return (restaurant) => {
          return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
        };
      },
      zhenduandata(formName) {
         this.$refs[formName].validate((valid) => {
                  if (valid) {
                    var sum = "";
                    for (var i = 0; i < this.zhenduanjieguo.description.length; i++) {
                      sum += this.zhenduanjieguo.description[i] + "、";
                    }
                    this.zhenduanjieguo.description = sum;
                    zhenduans(this.zhenduanjieguo).then(response => {
                      this.getList()
                      this.msgSuccess("用户诊断结束");
                      this.dialogFormVisible = false;
                    });
                  } else {
                   this.$message({
                            message: '请填写表单内容',
                            type: 'warning'
                          });
                    return false;
                  }
                });

      },
       resetForm2(formName) {
         this.dialogFormVisible=false
              this.$refs[formName].resetFields();
            }

    },
    mounted() {
      this.restaurantss = this.loadAll();
      this.restaurants = this.loadAll2();
    }
  };
</script>
